<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #operationTime="{ filterForm }">
        <el-form-item label="投用时间">
          <el-date-picker
            v-model="filterForm.time"
            type="daterange"
            range-separator="-"
            style="width: 100%"
            @change="operationTimeChange"
            format="MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['inspectionRoute:create']"
          >新增</el-button
        >
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      action-width="280"
    >
      <template #default>
        <el-table-column
          label="是否启用"
          width="200"
          prop="stopCheck"
          align="center"
          key="stopCheck"
        >
          <template #default="{ row }">
            {{ row.stopCheck == 0 ? "启用" : "停用" }}
          </template>
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button
          v-if="row.stopCheck == 0"
          type="text"
          @click="updateStopCheck(row, 1)"
          v-hasPermi="['inspectionRoute:updateStopCheck']"
          >停用</el-button
        >
        <el-button
          v-else
          type="text"
          @click="updateStopCheck(row, 0)"
          v-hasPermi="['inspectionRoute:updateStopCheck']"
          >启用</el-button
        >
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['inspectionRoute:update']"
          >修改</el-button
        >
        <el-button
          type="text"
          @click="handleDetail(row)"
          v-hasPermi="['inspectionRoute:queryPointList']"
          >路径规划</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['inspectionRoute:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :title="dialogType == 'add' ? '新增巡检路线' : '修改巡检路线'"
      :slots="['deptName', 'userName', 'chargePeople']"
    >
      <template #deptName>
        <el-form-item label="所属部门" prop="deptId" label-position="right">
          <TreeSelect
            v-model="form.deptId"
            :options="deptTree"
            @change="changeDept"
            style="width: 100%"
          >
          </TreeSelect>
        </el-form-item>
      </template>
      <template #userName>
        <el-form-item label="所属人" prop="userName" label-position="right">
          <el-input v-model="form.userName" readonly @click="onInnerOpen()">
          </el-input>
        </el-form-item>
      </template>
      <template #chargePeople>
        <el-form-item label="责任人" prop="chargePeople" label-position="right">
          <el-input
            v-model="form.chargePeople"
            readonly
            @click="onInnerOpen2()"
          >
          </el-input>
        </el-form-item>
      </template>
      <TableSelect
        ref="userTableSelect"
        :queryApi="listUser"
        :options="[]"
        :rowKey="'userId'"
        rowLabel="nickName"
        @onConfirm="onUserConfirm"
        rowLabelText="用户昵称"
        :maxNum="1"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="部门名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.deptName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.userName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="dept.deptName"
          />
          <el-table-column
            label="用户名称"
            align="center"
            key="userName"
            prop="userName"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            key="nickName"
            prop="nickName"
          />
        </template>
      </TableSelect>
    </DialogVform>
    <el-dialog v-model="outerVisible" width="90%" title="巡检任务详情">
      <Inner ref="inner" :parentNode="parentNode"></Inner>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/znxj/xjlx.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJsonNew } from "@/views/hooks/useAsyncData.js";
import TableSelect from "@/components/tableSelect/index";
import { deptTreeSelect, listUser } from "@/api/system/user";
import Inner from "./components//inner.vue";

const { proxy } = getCurrentInstance();
const ops = useFormJsonNew(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["routeCode", "routeName", "chargePeople"].includes(i.prop)
  ),
});

const showSearch = ref(true);
const dialogVform = ref();
const table = ref();
const userTableSelect = ref();
const changeDept = (val, label) => {
  form.value.deptName = label;
  form.value.deptId = val;
};
const updateStopCheck = (data, stopCheck) => {
  APIS.updateStopCheck({
    id: data.id,
    stopCheck,
  }).then((res) => {
    getList();
  });
};
const userType = ref("");
const onInnerOpen2 = () => {
  userType.value = "chargePeople";
  const { chargePeople } = form.value;
  // let values = (userId && userId.split(",")) || [];
  let names = (chargePeople && chargePeople.split(",")) || [];

  userTableSelect.value.open(
    names.map((item, index) => ({
      // value: values[index],
      label: names[index],
    }))
  );
};
const onInnerOpen = () => {
  userType.value = "userName";

  const { userId, userName } = form.value;
  let values = (userId && userId.toString().split(",")) || [];
  let names = (userName && userName.split(",")) || [];

  userTableSelect.value.open(
    values.map((item, index) => ({
      value: values[index],
      label: names[index],
    }))
  );
};
const onUserConfirm = (data) => {
  const values = data.map((item) => item.value);
  const names = data.map((item) => item.label);
  if (userType.value == "userName") {
    form.value.userId = values.join(",");
    form.value.userName = names.join(",");
  } else {
    form.value.chargePeople = names.join(",");
  }
};
// 补充修改.新增事件
const plugEdit = (row) => {
  form.value.deptName = row.deptName;
  form.value.deptId = row.deptId;
  form.value.userId = row.userId;
  form.value.userName = row.userName;
  form.value.chargePeople = row.chargePeople;
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy, plugEdit });
const deptTree = ref([]);
onMounted(() => {
  deptTreeSelect().then((response) => {
    deptTree.value = response.data;
  });
});

// 点位相关逻辑
const inner = ref();
const parentNode = ref({});
const outerVisible = ref(false);
const handleDetail = (row) => {
  outerVisible.value = true;
  nextTick().then(() => {
    parentNode.value = row;
  });
};
</script>

<style lang="scss" scoped></style>
